package routers

import (
	"helloCms/controllers"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
	"helloCms/models"
	"github.com/astaxie/beego/orm"
)


func init() {
	filterFunc:= func(ctx *context.Context) {
		userId:=ctx.Input.Session("userId").(int)
		//userName:=ctx.Input.Session("userName").(string)
		//if userName=="111"{
		//	return
		//}
		if userId == 0 {
			ctx.Redirect(302,"/Login/Index")
		}
		url:=ctx.Request.URL.Path//获取用户请求的url地址
		httpMethod:=ctx.Request.Method//获取用户请求方法
		//beego.Info(url,httpMethod)
		action:=models.Actioninfo{Url:url,HttpMethod:httpMethod}
		o:=orm.NewOrm()
		err:=o.Read(&action,"Url","HttpMethod")
		if err!=nil{
			beego.Info(err)
			ctx.WriteString("没有这种权限！")
			return
		}
		userActions:=make([]models.Useraction,0)
		if action.ActionTypeEnum==1 { //判断是否为普通权限
			o.QueryTable("useraction").Filter("actions_id",action.Id).One(&userActions)
			if len(userActions)>0{ //查到了用户-权限 中有该用户的权限设置
				for _,userAction:=range userActions{
					if userAction.IsPass==0{//判断权限是否被禁用
						continue
					}else { //userAction.IsPass==1
						return
					}
				}
			}else {//没有查到用户-权限 中有该用户的权限设置
				user:=models.Userinfo{Id:userId}
				o.LoadRelated(&user,"Roles")
				for _,role:=range user.Roles{
					o.LoadRelated(role,"Actions")
					for _,v:=range role.Actions{
						if v.Id==action.Id{
							return
						}
					}
				}
				ctx.WriteString("没有权限！")
			}
		}
	}

	beego.InsertFilter("/Admin/*",beego.BeforeExec,filterFunc)

	beego.Router("/Admin/UserInfo/Index",&controllers.UserInfoController{},"get:Index")
	beego.Router("/Admin/UserInfo/GetUserInfo",&controllers.UserInfoController{},"post:GetUserInfo")
    beego.Router("/Admin/UserInfo/AddUser",&controllers.UserInfoController{},"post:AddUser")
    beego.Router("/Admin/UserInfo/DeleteUser",&controllers.UserInfoController{},"post:DeleteUser")
    beego.Router("/Admin/UserInfo/EditUser",&controllers.UserInfoController{},"post:EditUser")
    beego.Router("/Admin/UserInfo/ShowSetUserRole",&controllers.UserInfoController{},"get:ShowSetUserRole")
    beego.Router("/Admin/UserInfo/EditUserRole",&controllers.UserInfoController{},"post:EditUserRole")
    beego.Router("/Admin/UserInfo/ShowUserAction",&controllers.UserInfoController{},"get:ShowUserAction")
    beego.Router("/Admin/UserInfo/SetUserAction",&controllers.UserInfoController{},"post:SetUserAction")
    beego.Router("/Admin/UserInfo/DeleteUserAction",&controllers.UserInfoController{},"post:DeleteUserAction")
    //---------------------------------------------------------------------------------
    beego.Router("/Admin/RoleInfo/Index",&controllers.RoleInfoController{},"get:Index")
	beego.Router("/Admin/RoleInfo/GetRoleInfo",&controllers.RoleInfoController{},"post:GetRoleInfo")
    beego.Router("/Admin/RoleInfo/AddRole",&controllers.RoleInfoController{},"post:AddRole")
    beego.Router("/Admin/RoleInfo/ShowAddRole",&controllers.RoleInfoController{},"get:ShowAddRole")
    beego.Router("/Admin/RoleInfo/DeleteRole",&controllers.RoleInfoController{},"post:DeleteRole")
    beego.Router("/Admin/RoleInfo/ShowSetRoleAction",&controllers.RoleInfoController{},"get:ShowSetRoleAction")
    beego.Router("/Admin/RoleInfo/SetRoleAction",&controllers.RoleInfoController{},"post:SetRoleAction")
	//---------------------------------------------------------------------------------
	beego.Router("/Admin/ActionInfo/Index",&controllers.ActionController{},"get:Index")
	beego.Router("/Admin/ActionInfo/GetActionInfo",&controllers.ActionController{},"post:GetActionInfo")
    beego.Router("/Admin/ActionInfo/AddAction",&controllers.ActionController{},"post:AddAction")
    beego.Router("/Admin/ActionInfo/FileUp",&controllers.ActionController{},"post:FileUp")
	//---------------------------------------------------------------------------------
	beego.Router("/Admin/Home/Index",&controllers.HomeController{},"get:Index")
	beego.Router("/Admin/Home/GetMenus",&controllers.HomeController{},"post:GetMenus")
	//---------------------------------------------------------------------------------
	beego.Router("/Login/Index",&controllers.LoginController{},"get:Index")
    beego.Router("/Login/UserLogin",&controllers.LoginController{},"post:UserLogin")
	//---------------------------------------------------------------------------------
	beego.Router("/Admin/ArticleClass/Index",&controllers.ArticleClassController{},"get:Index")
	beego.Router("/Admin/ArticleClass/ShowArticleClass",&controllers.ArticleClassController{},"post:ShowArticleClass")
	beego.Router("/Admin/ArticleClass/ShowChildClass",&controllers.ArticleClassController{},"post:ShowChildClass")
	beego.Router("/Admin/ArticleClass/ShowAddParentClass",&controllers.ArticleClassController{},"get:ShowAddParentClass")
	beego.Router("/Admin/ArticleClass/AddParentClass",&controllers.ArticleClassController{},"post:AddParentClass")
	beego.Router("/Admin/ArticleClass/ShowAddChildClass",&controllers.ArticleClassController{},"get:ShowAddChildClass")
	beego.Router("/Admin/ArticleClass/AddChildClass",&controllers.ArticleClassController{},"post:AddChildClass")
	//---------------------------------------------------------------------------------
	beego.Router("/Admin/ArticleInfo/Index",&controllers.ArticleInfoController{},"get:Index")
	beego.Router("/Admin/ArticleInfo/GetArticleInfo",&controllers.ArticleInfoController{},"post:GetArticleInfo")
	beego.Router("/Admin/ArticleInfo/ShowAddArticleInfo",&controllers.ArticleInfoController{},"get:ShowAddArticleInfo")
	beego.Router("/Admin/ArticleInfo/FileUp",&controllers.ArticleInfoController{},"post:FileUp")
	beego.Router("/Admin/ArticleInfo/AddArticle",&controllers.ArticleInfoController{},"post:AddArticle")





}
